﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Escape
{

    enum direction
    {
        z, x
    }

    class SpriteBlock: Block
    {
        public Texture2D tex;

        public Matrix world = Matrix.Identity;

        RasterizerState rs, rs2;

        direction d; 
        VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[6];
        VertexBuffer vb;


        public SpriteBlock(Vector3 position, direction dir, Texture2D tex)
        {
            sprite = true;
            this.tex = tex;

            d = dir;

            rs = new RasterizerState();
            rs2 = new RasterizerState();

            if (dir == direction.z)
            {
                collisionDistanceZ = -0.3f;
                collisionDistanceX = 0.2f;
            }
            else
            {
                collisionDistanceZ = 0.2f;
                collisionDistanceX = -0.3f;
            }

            rs.CullMode = CullMode.None;

            this.position = position;

            setUpV();
        }

        public override void alterRender()
        {


            Basic.device.RasterizerState = rs;
            Basic.device.SetVertexBuffer(vb);

           

            GameScreen.effect.Texture = tex;

            GameScreen.effect.World = Matrix.CreateTranslation(position);

            GameScreen.effect.CurrentTechnique.Passes[0].Apply();
            Basic.device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);

            base.alterRender();

            Basic.device.RasterizerState = rs2;
        }


        private void setUpV()
        {
            v[0] = new VertexPositionNormalTexture(new Vector3(0, -1, 0.0f), Vector3.Backward, new Vector2(0.0f, 1.0f));
            v[1] = new VertexPositionNormalTexture(new Vector3(0, 0, 0.0f), Vector3.Backward, new Vector2(0.0f, 0.0f));
            v[2] = new VertexPositionNormalTexture(new Vector3(1, -1, 0.0f), Vector3.Backward, new Vector2(1.0f, 1.0f));

            v[3] = new VertexPositionNormalTexture(new Vector3(1, -1, 0.0f), Vector3.Backward, new Vector2(1.0f, 1.0f));
            v[4] = new VertexPositionNormalTexture(new Vector3(0, 0, 0.0f), Vector3.Backward, new Vector2(0.0f, 0.0f));
            v[5] = new VertexPositionNormalTexture(new Vector3(1, 0, 0.0f), Vector3.Backward, new Vector2(1.0f, 0.0f));

            if (d == direction.z)
            {
                for (int i = 0; i < 6; i++)
                    v[i].Position.Z += 0.5f;
            }
            else
            {
                for (int i = 0; i < 6; i++)
                    v[i].Position.Y += -1.0f;
            }

            vb = new VertexBuffer(Basic.device, typeof(VertexPositionNormalTexture), 6, BufferUsage.WriteOnly);
            vb.SetData(v);
        }
    }
}
